#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

void ReverseWord(std::string &s,int start, int end)
{
    int p = start;
    int q = end;
    if (q > s.length())
    {
        return;
    }
    while(q > p)
    {
        if (s[p] != s[q])
        {
            s[p] = s[q] ^ s[p];
            s[q] = s[q] ^ s[p];
            s[p] = s[q] ^ s[p];
        }
        
        p++;
        q--;
    }
}

void Reverse2(std::string &s)
{
    std::string::iterator it1;
    std::string::iterator it2;

    it1 = s.begin();
    it2 = s.end();
    it2--;
    
    while(it1 < it2)
    {
        if (*it1 != *it2)
        {
            *it1 = *it2 ^ *it1;
            *it2 = *it2 ^ *it1;
            *it1 = *it2 ^ *it1;
        }
        
        it1 ++;
        it2 --;
    }
}
void removeSpace(std::string &s)
{
    int i = 0;
    int j = 0;
    int l = s.length();
    s[l] = '\0';
    while(j < l)
    {
        while(j < l && s[j] == ' ')
        {
            j++;
        } 
        while(j < l && s[j] != ' ')
        {
            s[i++] = s[j++];
        }

        if (s[i-1] != ' ')
        {
            s[i++] = ' '; 
        }
        
    }
    s[--i] = '\0';
    s.erase(s.begin()+i, s.end());
}

void reverseWords(std::string &s) {
    int i = 0;
    int j = 0;
    if (s.empty())
    {
        return;
    }
    removeSpace(s);
    while(s[j] != '\0')
    {
        
        while(s[i] == ' ' && s[i] != '\0')
            i++;
        j = i;
        while(s[j] != ' ' && s[j] != '\0')
            j++;
        ReverseWord(s, i, j-1);
        i = j;
    }
    ReverseWord(s, 0, s.length() - 1);
}

int main(int argc, char const *argv[])
{
    std::string s("the blue sky    ");
    reverseWords(s);
    printf("%s\n", s.c_str());
    std::cin>>s;
    std::cout<< s.capacity() <<std::endl;
    std::cout<< s.length() << s <<std::endl;
    return 0;
}
